package com.pro.springbootshop.controller;

import com.pro.springbootshop.entityExtend.Search;
import com.pro.springbootshop.entityExtend.SearchReturn;
import com.pro.springbootshop.entity.Wares;
import com.pro.springbootshop.mapper.MainClassesMapper;
import com.pro.springbootshop.mapper.OtherClassesMapper;
import com.pro.springbootshop.mapper.WaresMapper;
import com.pro.springbootshop.service.WaresService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class WaresController {
    @Autowired
    WaresService waresService;
    public WaresMapper waresMapper;
    @Autowired
    public void setUserMapper (WaresMapper waresMapper) {
        this.waresMapper = waresMapper;
    }
    public OtherClassesMapper otherClassesMapper;
    public MainClassesMapper mainClassesMapper;
    @Autowired
    public void setUserMapper (MainClassesMapper mainClassesMapper) {
        this.mainClassesMapper = mainClassesMapper;
    }
    @Autowired
    public void setUserMapper (OtherClassesMapper otherClassesMapper) {
        this.otherClassesMapper = otherClassesMapper;
    }
    @ApiOperation("获取商品")
    @PostMapping("/api/wares")
    public SearchReturn findWares (@RequestBody Search search) {
        return waresService.findWares(search);
    }
    @ApiOperation("根据商品ID获取商品信息")
    @GetMapping("/api/wares/{waresId}")
    public Wares getWares (@PathVariable(name = "waresId") Integer waresId){
        return waresService.getWares(waresId);
    }
    @ApiOperation("下架商品")
    @PostMapping("/api/wares/delete/{waresId}")
    public String deleteWares (@PathVariable(name = "waresId") Integer waresId){
        return waresService.deleteWares(waresId);
    }
    @ApiOperation("上架商品")
    @PostMapping("/api/wares/insert")
    public String insertWares(@RequestBody Wares wares){
        return waresService.insertWares(wares);
    }
    @ApiOperation("修改商品")
    @PostMapping("/api/wares/update")
    public String updateWares(@RequestBody Wares wares){
        return waresService.updateWares(wares);
    }
    @ApiOperation("上传图片")
    @PostMapping("/api/wares/upload")
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam("sign") String sign, HttpServletRequest request)throws IOException {
        String path=request.getServletContext().getRealPath("/upload/");
        return waresService.upload(file,sign,path);
    }
}
